package com.item.yxlottery.yxlottery.activity;

/*
 *------------------------------
 * @创建者      Administrator
 * @创建时间    2016/11/16 09:59
 * @文件描述    主界面
 *------------------------------
 */

import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.flyco.animation.Attention.Swing;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.item.yxlottery.yxlottery.R;
import com.item.yxlottery.yxlottery.base.BaseActivity;
import com.item.yxlottery.yxlottery.common.MyApplication;
import com.item.yxlottery.yxlottery.fragment.AccountFragment;
import com.item.yxlottery.yxlottery.fragment.BuyLotteryFragment;
import com.item.yxlottery.yxlottery.fragment.StoreFragment;
import com.item.yxlottery.yxlottery.service.LocationService;
import com.item.yxlottery.yxlottery.views.AutoRadioGroup;
import com.zhy.autolayout.AutoFrameLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.item.yxlottery.yxlottery.R.id.rb_home_user;

public class HomeActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.afl_home_content)
    AutoFrameLayout mAfContent;//显示fragment的区域

    @Bind(R.id.rg_home_group)
    AutoRadioGroup mRgGroup;//底部切换的RadioGroup

    @Bind(R.id.rb_home_buylottery)
    RadioButton mRbBuylottery;

    @Bind(R.id.rb_home_store)
    RadioButton mRbStore;

    @Bind(rb_home_user)
    RadioButton mRbUser;

    private FragmentManager mManager;
    private ImageButton mIbActionbarLeft;
    private TextView mTvActionBarTitle;
    private ImageButton mIbActionbarRight;
    private AutoRelativeLayout mLayoutActionbar;
    private LocationService mLocationService;
    private MyLocationListener mMyLocationListener;
    private String mProvince = "";//定位得到的当前省
    private String mCity = "";//定位得到的当前市(地区)
    private BDLocation mBDLocation;

    @Override
    protected void setCustomActionBar(ImageButton ibActionbarLeft, TextView tvActionBarTitle, ImageButton ibActionbarRight, AutoRelativeLayout layoutActionbar) {
        mIbActionbarLeft = ibActionbarLeft;
        mTvActionBarTitle = tvActionBarTitle;
        mIbActionbarRight = ibActionbarRight;
        mLayoutActionbar = layoutActionbar;
        mLayoutActionbar.setVisibility(View.GONE);
    }

    @Override
    protected View initView() {
        View view = View.inflate(this, R.layout.activity_home, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        mRgGroup.check(R.id.rb_home_buylottery);//默认选中购彩RadioButton
        // 指定默认显示HomeFrament的内容
        mManager = getSupportFragmentManager();
        FragmentTransaction transaction = mManager.beginTransaction();
        transaction.replace(R.id.afl_home_content, new BuyLotteryFragment());
        transaction.commit();

        mLocationService = MyApplication.getLocationService();//获得监听服务
        mMyLocationListener = new MyLocationListener();//监听
        mLocationService.registerListener(mMyLocationListener);//注册监听
        mLocationService.start();//开始监听
    }

    @Override
    protected void initEvent() {
        mRgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                FragmentTransaction transaction = mManager.beginTransaction();
                switch (i) {
                    case R.id.rb_home_buylottery://购彩
                        mLayoutActionbar.setVisibility(View.GONE);
                        transaction.replace(R.id.afl_home_content, new BuyLotteryFragment());
                        break;
                    case R.id.rb_home_store://彩店
                        mLayoutActionbar.setVisibility(View.VISIBLE);
                        mTvActionBarTitle.setText("彩票店");
                        mIbActionbarLeft.setImageResource(R.drawable.leftarrow);
                        mIbActionbarRight.setImageResource(R.drawable.location2);
                        StoreFragment storeFragment = new StoreFragment();
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("location", mBDLocation);
                        storeFragment.setArguments(bundle);
                        transaction.replace(R.id.afl_home_content, storeFragment, "store");
                        break;
                    case rb_home_user://账户
                        mLayoutActionbar.setVisibility(View.VISIBLE);
                        mIbActionbarLeft.setImageResource(R.drawable.leftarrow);
                        mTvActionBarTitle.setText("我的");
                        mIbActionbarRight.setImageResource(R.drawable.setting);
                        transaction.replace(R.id.afl_home_content, new AccountFragment());
                        break;
                }
                transaction.commit();
            }
        });
        mIbActionbarLeft.setOnClickListener(this);
    }

    @Override
    public void onStop() {
        mLocationService.stop();//停止定位监听
        mLocationService.unregisterListener(mMyLocationListener);
        super.onStop();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {//手动选择位置传值过来所需
        if (resultCode == RESULT_OK) {
            Fragment storefragment = mManager.findFragmentByTag("store");
            storefragment.onActivityResult(requestCode, resultCode, data);
        }
    }

    /*- 返回键监听 -*/
    @Override
    public void onBackPressed() {
        switch (mRgGroup.getCheckedRadioButtonId()) {
            case R.id.rb_home_buylottery://只有在购彩fragment里面返回才退出程序
                final NormalDialog dialog = new NormalDialog(this);
                dialog.content("亲,真的要走吗?再看会儿吧~(●—●)")//
                        .style(NormalDialog.STYLE_TWO)//
                        .titleTextSize(20)//
                        .titleTextColor(0x770000ff)
                        .btnText("继续逛逛", "残忍退出")//
                        .btnTextColor(0xff000000, 0xff000000)//
                        .btnTextSize(16f, 16f)//
                        .showAnim(new Swing())//
                        .dismissAnim(new SlideBottomExit())//
                        .show();
                dialog.setOnBtnClickL(
                        new OnBtnClickL() {
                            @Override
                            public void onBtnClick() {
                                dialog.dismiss();
                            }
                        },
                        new OnBtnClickL() {
                            @Override
                            public void onBtnClick() {
                                dialog.superDismiss();
                                finish();
                                System.exit(0);
                                Process.killProcess(Process.myPid());
                            }
                        });
                break;
            case R.id.rb_home_store:
            case R.id.rb_home_user:
                mRgGroup.check(R.id.rb_home_buylottery);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_actionbar_left:
                mRgGroup.check(R.id.rb_home_buylottery);
                break;
        }
    }

    /*- 自定义监听 -*/
    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            mBDLocation = bdLocation;
        }
    }
}